package com.cuile.littletools.scan

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.RemoteViews
import com.cuile.littletools.R

/**
 * Created by cuile on 18-12-21.
 *
 */
class ScanWidgetProvider : AppWidgetProvider() {
    override fun onUpdate(context: Context?, appWidgetManager: AppWidgetManager?, appWidgetIds: IntArray?) {
        super.onUpdate(context, appWidgetManager, appWidgetIds)
        appWidgetIds?.forEach { appWidgetId ->


            val intent = Intent(Intent.ACTION_VIEW, Uri.parse("cuile://www.littletool/scanresult")).apply {
                //flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME
//                putExtra("openType", "desktop")
            }

            val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

            val remoteView = RemoteViews(context?.packageName, R.layout.scan_widget_layout).apply {
                setOnClickPendingIntent(R.id.scan_widget_ImageBtn, pendingIntent)
            }

            appWidgetManager?.updateAppWidget(appWidgetId, remoteView)
        }


    }
}